package cn.wolfcode.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.mapper.AppointmentMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IAppointmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper AppointmentMapper;


    @Override
    public void save(Appointment appointment) {
        //保存前需要设置状态,创建时间和流水号
        //设置创建时间
        appointment.setCreateTime(new Date());

        //设置流水号,均使用HuTool工具类的方法
        //时间字符串
        String date1 = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        //随机五个数字
        String data2 = RandomUtil.randomNumbers(5);
        appointment.setAno(date1+data2);
        AppointmentMapper.insert(appointment);
    }

    @Override
    public void delete(Long id) {
        AppointmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Appointment appointment) {
        AppointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return AppointmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Appointment> listAll() {
        return AppointmentMapper.selectAll();
    }

    @Override
    public PageInfo<Appointment> query(QueryObject qo) {
        //对状态和预约时间排序
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"status,appointment_time");
        List<Appointment> appointments = AppointmentMapper.selectForList(qo);
        return new PageInfo<>(appointments);
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        AppointmentMapper.changeStatus(id,status);
    }
}
